<?php
html::printPath(
    array(
        'viSeminar.form-title' => 'admin/viSeminar/overview',
        $viSeminar['name'] => array(
            'admin/viSeminar/display?id=' . $viSeminar['id'],
            'viSeminar.assignAccomodation' => 'admin/viSeminarAssignAccomodation/form?id=' . $viSeminar['id'],
            'viSeminar.seminarBillTable' => 'admin/viSeminar/billTable?id=' . $viSeminar['id']
        ),
        'viSeminar.viRegistrationList-title' => ''
    ),
    'viSeminar.viRegistrationList-desc'
);
$msg->printMessages();

if (count($viRegistrationList) == 0) {
    echo text::getEscaped('viSeminar.noRegistrations');
}
else {
    $presentCount = 0;
    $notPresentCount = 0;
    ?>

    <ul class="commandList">
        <li class="exportToExcel">
            <?php echo html::slideToggle(text::get('viSeminar.mealTableForKitchen'), 'kitchenTableOptions', text::get('viSeminar.mealTableForKitchen-title')); ?>
            <ul id="kitchenTableOptions" class="hidden">
                <li><?php echo html::anchor('admin/viSeminar/mealTableForKitchen?mode=present&id=' . $viSeminar['id'], text::get('viSeminar.participants-presentOnly')) ?></li>
                <li><?php echo html::anchor('admin/viSeminar/mealTableForKitchen?mode=all&id=' . $viSeminar['id'], text::get('viSeminar.participants-all')) ?></li>
            </ul>
        </li>
        <li class="emailExportCommand">
            <?php echo html::slideToggle(text::get('viSeminar.exportEmails'), 'emailExportOptions', text::get('viSeminar.exportEmails-title')) ?>
            <ul id="emailExportOptions" class="hidden">
                <li><?php echo html::anchor('admin/viSeminar/exportEmails?mode=present&id=' . $viSeminar['id'], text::get('viSeminar.participants-presentOnly')) ?></li>
                <li><?php echo html::anchor('admin/viSeminar/exportEmails?mode=all&id=' . $viSeminar['id'], text::get('viSeminar.participants-all')) ?></li>
            </ul>
        </li>
    </ul>

    <?php
    function writeRegistrationTable($list, $cancelled) {
        global $presentCount, $notPresentCount;
        ?>
        <table class="listTable accomodationTable">
            <thead>
                <tr>
                    <?php
                    if (! $cancelled) {
                        $headerKeyList = array(
                            'viRegistration.id-short',
                            'viRegistration.is_present-short',
                            'person.full_name',
                            'person.birth_date',
                            'viRegistration.room_number',
                            'viRegistration.key_deposit',
                            'viRegistration.vi_accomodation_type_code',
                            'viRegistration.total_price',
                            'viRegistration.paid_total',
                            'viRegistration.accounting_record_numbers',
                            'viRegistration.registration_notes',
                            'viRegistration.person_notes'
                        );
                    }
                    else {
                        $headerKeyList = array(
                            'viRegistration.id-short',
                            'person.full_name',
                            'person.birth_date',
                            'viRegistration.vi_accomodation_type_code',
                            'viRegistration.total_price',
                            'viRegistration.paid_total',
                            'viRegistration.accounting_record_numbers',
                            'viRegistration.registration_notes',
                            'viRegistration.person_notes',
                            'viRegistration.cancelled_at',
                        );
                    }
                    foreach ($headerKeyList as $headerKey) {
                        echo '<th>' . text::getEscaped($headerKey) . '</th>';
                    }
                    ?>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($list as $viRegistration) {
                    if ($viRegistration['is_present']) {
                        $presentCount ++;
                    }
                    else {
                        $notPresentCount ++;
                    }

                    $currencyCode = $viRegistration['currency_code'];
                    $currencyMark = text::get('currency.mark-' . $currencyCode);
                    $paidAmount = 0;
                    foreach ($viRegistration['paymentList'] as $payment) {
                        $paidAmount = bcadd($paidAmount, $payment['paid_amount'], sql::$DECIMAL_PRECISION);
                    }

                    echo form::hidden('regIdList[]', $viRegistration['id']);
                    echo '<tr>';
                    echo '<td>' . html::moreInfoPopup('/admin/viRegistration/display?id=' . $viRegistration['id'],
                            $viRegistration['id']) . '</td>';
                    if (! $cancelled) {
                        echo '<td>' . html::anchor('#',
                                format::twoValueBoolean($viRegistration['is_present']),
                                array(
                                    'title' => text::get('viRegistration.togglePresence'),
                                    'onclick' => "Yosemin.Ajax.partialPageUpdate('" . url::site('/admin/viRegistration/togglePresence') . "', null, {id:" . $viRegistration['id'] . "}, null); return false;",
									'id' => 'presence_' . $viRegistration['id']
                                )) . '</td>';
                    }
                    echo '<td class="textLeft">' . html::moreInfoPopup('/admin/person/display?id=' . $viRegistration['person_id'],
                            $viRegistration['last_name'] . ' ' . $viRegistration['first_name']) . '</td>';
                    echo '<td>' . format::date($viRegistration['birth_date']) . '</td>';
                    if (! $cancelled) {
                        echo '<td>' . $viRegistration['room_number'] . '</td>';
                        $popupUrl = '/admin/viRegistration/keyDepositForm?id=' . $viRegistration['id'];
                        echo '<td>';
                        if ($viRegistration['key_deposit']) {
                            $linkAttributes = array();
                            if ($viRegistration['is_key_returned']) {
                                $linkAttributes['class'] = 'keyReturnedMark';
                                $linkAttributes['title'] = text::get('viRegistration.keyHasBeenReturned');
                            }
                            echo html::popupForm($popupUrl, format::decimal($viRegistration['key_deposit']) . ' ' . $currencyMark, $linkAttributes);
                        }
                        else {
                            echo html::popupForm($popupUrl, '&ndash;');
                        }
                        echo '</td>';
                    }
                    echo '<td class="textLeft">' . $viRegistration['vi_accomodation_type_name'] . '</td>';
                    echo '<td>' . format::decimal($viRegistration['total_price']) . ' ' . $currencyMark . '</td>';
                    if ($paidAmount != 0) {
                        $paymentDiff = bcsub($paidAmount, $viRegistration['total_price'], sql::$DECIMAL_PRECISION);
                        $paidAmountClass = '';
                        $paidAmountDesc = '';
                        if ($paymentDiff != 0) {
                            $paidAmountClass = 'icorrectPayment';
                            if ($paymentDiff > 0) {
                                $paidAmountDesc = text::get('viRegistration.overpaidAmount') . ': ' .
                                        format::decimal($paymentDiff) . ' ' . $currencyMark;
                            }
                            else {
                                $paidAmountDesc = text::get('viRegistration.underpaidAmount') . ': ' .
                                        format::decimal($paymentDiff) . ' ' . $currencyMark;
                            }
                        }
                        echo '<td class="' . $paidAmountClass . '" title="' . $paidAmountDesc . '">' .
                                format::decimal($paidAmount) . ' ' . $currencyMark . '</td>';
                    }
                    else {
                        echo '<td>&ndash;</td>';
                    }
                    echo '<td style="font-size: small">';
                    for ($i = 0; $i < count($viRegistration['paymentList']); $i++) {
                        $payment = $viRegistration['paymentList'][$i];
                        $paymentName = $payment['record_number'];
                        if (! $paymentName) {
                            $paymentName = format::decimal($payment['paid_amount']);
                        }
                        echo html::moreInfoPopup('/admin/viRegistration/paymentDisplay?id=' . $payment['id'], $paymentName);
                        if ($i < count($viRegistration['paymentList']) - 1) {
                            echo ', ';
                        }
                        else {
                            echo ' ';
                        }
                    }
                    if (! $cancelled) {
                        echo html::popupForm('/admin/viRegistration/paymentCreate?viRegistrationId=' . $viRegistration['id'],
                                html::img('add.png'),
                                array('title' => text::get('viRegistration.addNewPayment')));
                    }
                    echo '</td>';

                    $notesTitle = $viRegistration['notes'];
                    $notes = string::longTextShort($notesTitle);
                    if ($notes == $notesTitle) {
                        $notesTitle = '';
                    }
                    echo '<td class="textLeft" style="font-size: small" title="' . html::specialchars($notesTitle) . '">' . html::specialchars($notes) . '</td>';

                    $notesTitle = $viRegistration['person_notes'];
                    $notes = string::longTextShort($notesTitle);
                    if ($notes == $notesTitle) {
                        $notesTitle = '';
                    }
                    echo '<td class="textLeft" style="font-size: small" title="' . html::specialchars($notesTitle) . '">' . html::specialchars($notes) . '</td>';
                    if ($cancelled) {
                        echo '<td class="textRight">' . format::timestamp($viRegistration['cancelled_at']) . '</td>';
                    }
                    echo '</tr>';
                }
                ?>
            </tbody>
        </table>
        
        <table class="pricesTable">
            <tbody>
                <tr>
                    <th><?php echo text::getEscaped('viSeminar.presentCount') ?></th>
                    <td><?php echo $presentCount ?></td>
                </tr>
                <tr>
                    <th><?php echo text::getEscaped('viSeminar.notPresentCount') ?></th>
                    <td><?php echo $notPresentCount ?></td>
                </tr>
            </tbody>
        </table>
        <?php
    }

    writeRegistrationTable($viRegistrationList, FALSE);

    if ($cancelledRegistrationList) {
        echo '<h2>' . text::getEscaped('viSeminar.cancelledRegistrations') . '</h2>';
        writeRegistrationTable($cancelledRegistrationList, TRUE);
    }
}